package de.suur.jarmi.client;

import java.io.IOException;
import java.net.Socket;

import de.suur.jarmi.common.Compressor;
import de.suur.jarmi.common.JarmiException;
import de.suur.jarmi.common.Serializer;

public class JarmiSocketClient extends JarmiClient {

	private String host;
	private int port;

	public JarmiSocketClient(Class<?> serviceInterface, Compressor compressor,
			Serializer serializer, String host, int port) {
		super(serviceInterface, compressor, serializer);
		this.host = host;
		this.port = port;
	}

	@Override
	protected byte[] request(byte[] requestData) {
		Socket socket;
		try {
			socket = new Socket(host, port);
			if (socket.isConnected()) {
				sendRequest(requestData, socket.getOutputStream());
				byte[] response = receiveResponse(socket.getInputStream());
				socket.close();
				return response;
			}
		} catch (IOException e) {
			throw new JarmiException(e);
		}
		return null;
	}

}
